package com.yc.jpyy.jcanjuan.service.imp;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.yc.jpyy.base.service.BaseService;
import com.yc.jpyy.jcanjuan.dao.JCAnJuanDao;
import com.yc.jpyy.jcanjuan.service.JCAnJuanService;

@Service
public class JCAnJuanServiceImpl extends BaseService implements JCAnJuanService {
 

@Autowired
private JCAnJuanDao jCAnJuanDao;

@Override
public Map<String, Object> addJCAnJuan(Map<String, Object> paramsMap)
		throws DataAccessException {
	Map<String,Object> resultMap = new HashMap<>();
	int k = jCAnJuanDao.addJCAnJuan(paramsMap);
	if(k < 0){
		resultMap.put("returnCode", 0);
		resultMap.put("returnMsg", "新增失败");
	}else{
		resultMap.put("returnCode", 1);
		resultMap.put("returnMsg", "新增成功");
	}		
	return resultMap;
}


@Override
public Map<String, Object> selectJCAnJuanById(Map<String, Object> paramsMap) throws DataAccessException {
	Map<String,Object> resultMap = new HashMap<>();
	Map<String,Object> info = jCAnJuanDao.selectJCAnJuanById(paramsMap);
	if(info != null){
		resultMap.put("returnCode", 1);
		resultMap.put("ListData", info);
		resultMap.put("returnMsg", "操作成功");
	}else{
		resultMap.put("returnCode", 0);
		resultMap.put("returnMsg", "系统查询不到该条信息");
	}
	return resultMap;
}

@Override
public Map<String, Object> selectJCAnJuanByJcid(Map<String, Object> paramsMap) throws DataAccessException {
	Map<String,Object> resultMap = new HashMap<>();
	Map<String,Object> info = jCAnJuanDao.selectJCAnJuanByJcid(paramsMap);
	if(info != null){
		resultMap.put("returnCode", 1);
		resultMap.put("ListData", info);
		resultMap.put("returnMsg", "操作成功");
	}else{
		resultMap.put("returnCode", 0);
		resultMap.put("returnMsg", "系统查询不到该条信息");
	}
	return resultMap;
}


@Override
public Map<String, Object> updateJCAnJuan(Map<String, Object> paramsMap)
		throws DataAccessException {
	Map<String,Object> resultMap = new HashMap<>();
	int k = jCAnJuanDao.updateJCAnJuan(paramsMap);
	if(k < 0){
		resultMap.put("returnCode", 0);
		resultMap.put("returnMsg", "更新失败");
	}else{
		resultMap.put("returnCode", 1);
		resultMap.put("returnMsg", "更新成功");
	}
	return resultMap;
}

@Override
public Map<String, Object> getJcajNum(Map<String, Object> paramsMap)
		throws DataAccessException {
	Map<String,Object> resultMap = new HashMap<>();
	//查询该类型是否存在
	Map<String,Object> info = jCAnJuanDao.checkTypeIsHave(paramsMap);
	if(info != null){
		//检查取消号段是否有值
		String cancelNum = Optional.ofNullable(info.get("CancelNumber")).orElse("").toString();
		if(!cancelNum.equals("")){
			//有值则返回第一个值返回，并去掉第一个值重新更新到库中
			if(cancelNum.split(",").length > 1){
				paramsMap.put("CancelNumber", cancelNum.substring(cancelNum.indexOf(",")+1));	
			}else{
				paramsMap.put("CancelNumber", null);
			}
			int count = jCAnJuanDao.updateCancelNum(paramsMap);
			if(count < 0){
				resultMap.put("returnCode", 0);
				resultMap.put("returnMsg", "更新失败");
			}else{
				resultMap.put("returnCode", 1);
				resultMap.put("ListData", cancelNum.split(",")[0]);
				resultMap.put("returnMsg", "操作成功");
			}
		}else{
			//更新流水号加1
			int i = jCAnJuanDao.updateNum(paramsMap);
			if(i < 0){
				resultMap.put("returnCode", 0);
				resultMap.put("returnMsg", "更新失败");
			}else{
				resultMap.put("returnCode", 1);
				resultMap.put("ListData", Integer.valueOf(info.get("num").toString()) + 1);
				resultMap.put("returnMsg", "操作成功");
			}
		}	
	}else{
		//新增该类型的记录
		paramsMap.put("num", 1);
		int  j = jCAnJuanDao.addType(paramsMap);
		if(j  < 0){
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "添加失败");
		}else{
			resultMap.put("returnCode", 1);
			resultMap.put("ListData", 1);
			resultMap.put("returnMsg", "操作成功");
		}
	}
	return resultMap;
}

@Override
public Map<String, Object> revertJcajNum(Map<String, Object> paramsMap)
		throws DataAccessException {
	Map<String,Object> resultMap = new HashMap<>();
	int k = jCAnJuanDao.revertJcajNum(paramsMap);
	if(k < 0){
		resultMap.put("returnCode", 0);
		resultMap.put("returnMsg", "撤回失败");
	}else{
		resultMap.put("returnCode", 1);
		resultMap.put("returnMsg", "撤回成功");
	}
	return resultMap;
}
}
